'use client'

import { VmsCourseSection } from '@/types/Model'
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog'
import EnhancedSectionForm from './EnhancedSectionForm'

interface SectionEditDialogProps {
  section: VmsCourseSection | null
  open: boolean
  onOpenChange: (open: boolean) => void
  onSave: (sectionData: Partial<VmsCourseSection>) => Promise<void>
}

export default function SectionEditDialog({
  section,
  open,
  onOpenChange,
  onSave
}: SectionEditDialogProps) {
  const handleSubmit = async (sectionData: Partial<VmsCourseSection>) => {
    try {
      await onSave({
        ...sectionData,
        id: section?.id
      })
      onOpenChange(false)
    } catch (error) {
      console.error('保存失败:', error)
      // 这里可以添加错误提示
    }
  }

  const handleCancel = () => {
    onOpenChange(false)
  }

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-[500px] max-h-[80vh] overflow-y-auto">
        <DialogHeader>
          <DialogTitle>编辑小节</DialogTitle>
        </DialogHeader>
        
        <div className="py-4">
          <EnhancedSectionForm
            section={section}
            onCancel={handleCancel}
            onSubmit={handleSubmit}
          />
        </div>
      </DialogContent>
    </Dialog>
  )
}